package org.jeecg.modules.demo.hydrology.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.hydrology.dto.DbcxcdssDto;
import org.jeecg.modules.demo.hydrology.dto.DbcxyjDto;
import org.jeecg.modules.demo.hydrology.entity.Dbcxcdss;
import org.jeecg.modules.demo.hydrology.entity.Dbcxyj;
import org.jeecg.modules.demo.hydrology.mapper.DbcxcddyMapper;
import org.jeecg.modules.demo.hydrology.mapper.DbcxyjMapper;
import org.jeecg.modules.demo.hydrology.service.IDbcxyjService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 地表沉陷预警
 * @Author: jeecg-boot
 * @Date:   2024-04-17
 * @Version: V1.0
 */
@Service
public class DbcxyjServiceImpl extends ServiceImpl<DbcxyjMapper, Dbcxyj> implements IDbcxyjService {
    @Autowired
    private DbcxyjMapper dbcxyjMapper;
    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    @Override
    public IPage<DbcxyjDto> getList(Page<Dbcxyj> page, QueryWrapper<Dbcxyj> queryWrapper) {
        Page<Dbcxyj> dbcxyjPage = dbcxyjMapper.selectPage(page, queryWrapper);
        List<DbcxyjDto> collect = dbcxyjPage.getRecords().stream().map(dbcxyj -> {
            DbcxyjDto dbcxyjDto = new DbcxyjDto();
            BeanUtils.copyProperties(dbcxyj, dbcxyjDto);
            QueryWrapper<SysDepart> sysDepartQueryWrapper = new QueryWrapper<>();
            sysDepartQueryWrapper.eq("org_code", dbcxyj.getStationMine());
            SysDepart sysDepart = sysDepartMapper.selectOne(sysDepartQueryWrapper);
            if (sysDepart != null){
                dbcxyjDto.setStationName(sysDepart.getDepartName());
            }
            QueryWrapper<SysDict> sysDictQueryWrapper = new QueryWrapper<>();
            sysDictQueryWrapper.eq("dict_code","alarm_status");
            SysDict sysDict = sysDictMapper.selectOne(sysDictQueryWrapper);
            if (sysDict != null){
                QueryWrapper<SysDictItem> sysDictItemQueryWrapper = new QueryWrapper<>();
                sysDictItemQueryWrapper.eq("dict_id",sysDict.getId()).eq("item_value",dbcxyjDto.getAlarmStatus());
                SysDictItem sysDictItem = sysDictItemMapper.selectOne(sysDictItemQueryWrapper);
                if (sysDictItem != null){
                    dbcxyjDto.setAlarmStatusItemText(sysDictItem.getItemText());
                }
            }
            return dbcxyjDto;
        }).collect(Collectors.toList());
        IPage<DbcxyjDto> dbcxyjDtoIPage = new Page<>();
        dbcxyjDtoIPage.setRecords(collect);
        dbcxyjDtoIPage.setTotal(dbcxyjPage.getTotal());
        dbcxyjDtoIPage.setPages(page.getPages());
        dbcxyjDtoIPage.setSize(page.getSize());
        return dbcxyjDtoIPage;
    }
}
